<?php

namespace App;

use App\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class AdminUser extends Authenticatable
{
    protected $rememberTokenName = '';
    protected $guarded = [];

    /*
     * 一个用户有哪些角色
     */
    public function roles()
    {
        return $this->belongsToMany(\App\AdminRole::class, 'admin_role_user', 'user_id', 'role_id')->withPivot(['user_id', 'role_id']);
    }

   	//判断是否有某个角色，某些角色
   	public function isInRoles($roles)
   	{
   		//intersect() 判断集合是否有交集
   		return !!$roles->intersect($this->roles)->count();
   	}

   	//给用户分配角色
   	public function assignRole($role)
   	{
   		return $this->roles()->save($role);
   	}

   	//取消用户分配的角色
   	public function deleteRole($role)
   	{
   		return $this->roles()->detach($role);
   	}

    /**
     * [hasPermission 用户是否有权限]
     * @param  [type]  $permission [description]
     * @return boolean             [description]
     */
    public function hasPermission($permission)
    {
      return $this->isInRoles($permission->roles);
    }
}
